package com.shuaiche.yeguan.ui.report;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.util.DeviceHelper;
import com.shuaiche.yeguan.views.DragGridBaseAdapter;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 类描述：
 *
 * @auther: baoyinbo
 * @date: 2019/4/16 5:12 PM
 */

public class SCCarEditUploadPicAdapter extends BaseAdapter implements DragGridBaseAdapter {
    public static final int TYPE_CAMERA = 0;
    public static final int TYPE_NORMAL = 1;
    private int mHidePosition = -1;

    private ArrayList<String> listUrls; //本地相册图片
    private LayoutInflater inflater;
    private Context mContext;

    private boolean showCamera = true;
    public int MAXSIZE = 16;


    public SCCarEditUploadPicAdapter(Context context, ArrayList<String> listUrls, int maxSize,
                                     boolean showCamera) {
        if (listUrls == null) listUrls = new ArrayList<String>();
        this.showCamera = showCamera;
        this.listUrls = listUrls;
        this.mContext = context;
        this.MAXSIZE = maxSize;
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        if (listUrls.size() == MAXSIZE) return MAXSIZE;
        return showCamera ? listUrls.size() + 1 : listUrls.size();
    }

    @Override
    public String getItem(int position) {
        if (showCamera && listUrls.size() < MAXSIZE) {
            if (position == (getCount() - 1)) return null;
        }
        return listUrls.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (listUrls.size() == MAXSIZE) return TYPE_NORMAL;
        if (showCamera) {
            return position == (getCount() - 1) ? TYPE_CAMERA : TYPE_NORMAL;
        }
        return TYPE_NORMAL;
    }

    @Override
    public void reorderItems(int oldPosition, int newPosition) {
        if (!showCamera || (oldPosition != getCount() - 1 && newPosition != getCount() - 1)) {
                String temp = listUrls.get(oldPosition);
                if (oldPosition < newPosition) {
                    for (int i = oldPosition; i < newPosition; i++) {
                        Collections.swap(listUrls, i, i + 1);
                    }
                } else if (oldPosition > newPosition) {
                    for (int i = oldPosition; i > newPosition; i--) {
                        Collections.swap(listUrls, i, i - 1);
                    }
                }
                listUrls.set(newPosition, temp);


        }
    }

    @Override
    public void setHideItem(int hidePosition) {
        this.mHidePosition = hidePosition;
        notifyDataSetChanged();
    }

    @Override
    public void removeItem(int removePosition) {

    }

    public  interface deleteCallBackListener {
        void delete(int index);
   }

    deleteCallBackListener listener;

    public deleteCallBackListener getListener() {
        return listener;
    }

    public void setListener(deleteCallBackListener listener) {
        this.listener = listener;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        int type = getItemViewType(position);
        if (type == TYPE_CAMERA) {
            view = inflater.inflate(R.layout.sc_item_upload_picture_car_add_grid, viewGroup, false);
            view.setTag(null);
        } else if (type == TYPE_NORMAL) {
            if (view == null) {
                view = inflater.inflate(R.layout.sc_item_upload_picture_grid, viewGroup, false);
            } else {
                view = inflater.inflate(R.layout.sc_item_upload_picture_grid, viewGroup, false);
            }
            ImageView image = (ImageView) view.findViewById(R.id.imageView);
            RelativeLayout relativeLayout = view.findViewById(R.id.rl_close);

            ViewGroup.LayoutParams params = image.getLayoutParams();
            int picWidth = DeviceHelper.getScreenWidthPx(mContext)/4;
            params.height = picWidth-50;
            image.setLayoutParams(params);

            final String path = listUrls.get(position);
            if (!showCamera) {
                relativeLayout.setVisibility(View.GONE);
            }else{
                relativeLayout.setVisibility(View.VISIBLE);

            }
            relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    listUrls.remove(position);
//                    notifyDataSetChanged();
                    listener.delete(position);
                }
            });
            //本地相册的图

            Glide.with(mContext)
                    .load(path)
                    .centerCrop()
                    .placeholder(R.mipmap.default_error)
                    .error(R.mipmap.default_error)
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .into(image);
        }
        if (position == mHidePosition) {
            view.setVisibility(View.INVISIBLE);
        }


        return view;
    }

}
